package graph_viewer.util;

import java.awt.*;
import java.io.*;
import java.util.*;

/**
 * define a file reader methods
 */

public class File_Util{	      
	static final public String log	= "log.txt";


	public static void main(String args[]){
		String[] b_t = parse_ts_input( "U	^User;personal information ; user information" );

		Debug.println( b_t[0] + "|" + b_t[1], 2) ;

		String a_string_line	= "information visualization, operators, user interactions,view/value, framework, spreadsheet, design, extensibility, visualization systems";
		ArrayList a_string_list	= parse_string_line( a_string_line, "," );

		Out.println( a_string_list.toString() );
	}

	/**
		the substring following the last "." is believed to be the file type
	*/
	static public String get_file_type( String file_name ){
		String clean_file_name	= file_name.trim();

		int the_last_dot	= clean_file_name.lastIndexOf( "." );

		return clean_file_name.substring( the_last_dot + 1 );
	}

	static public String get_file_name( String file_path_name ){
		String clean_file_name	= file_path_name.trim();

		int the_last_dot	= clean_file_name.lastIndexOf( "\\" );

		return clean_file_name.substring( the_last_dot + 1 );
	
	}

	// one line: 
	// brief_text full=text=has=this=format
	// =: new line
	// return Sting[2]: brief_text, text
	static public  String[] parse_ts_input( String whole_line ){
		StringTokenizer st = new StringTokenizer( whole_line, "^");// "graph_viewer.structure.Structure_Values.NODE_BRIFE_SEP" );

		String
			brief, text;

		if ( st.countTokens() < 2 ){
			brief	= whole_line;
			text	= whole_line;
		}else{
			brief	= st.nextToken().trim();
			text	=  st.nextToken().trim();
		}

		return new String[]{ brief.trim(), text.trim()};

	}
	
	//append to a file
	static public void append_to_file( String file_name, String text ) throws IOException{
		append_to_file( new File( file_name ), text );
	}

	static public void append_to_file( File file, String text ) throws IOException{
		/*if (file.canWrite() ){
			System.out.println( file + " can write."  + text);
		}else{
			System.out.println( file + " can NOT write." + text);
		}*/


		FileWriter writer	= new FileWriter( file , true );

		for ( int i = 0 ; i < text.length(); i++){
			int ch = (int)text.charAt( i );
			writer.write( ch );
		}	

		writer.write( '\n' );

		writer.flush();
		writer.close();
	}

	static public void batch_append_to_file( String file_name, Collection texts ) throws IOException{
		for ( Iterator _i = texts.iterator(); _i.hasNext(); ){
			String a_text_line	= (String)_i.next();

			append_to_file( file_name, a_text_line );
		}
	}

	static public void delete_file( String file_name ){
		new File( file_name ).delete();			
	}
	
	//one line to an element of arrayList
	static public ArrayList read_file( String a_file_name ){
		ArrayList lines	= new ArrayList();

		try{
			File a_file	 = new File( a_file_name );
			BufferedReader	node_data_reader	= new BufferedReader( new InputStreamReader( new FileInputStream( a_file ) ) );			
			
			while ( true ){
				String node_list_string = node_data_reader.readLine();	

				if ( node_list_string == null ){   
					break;
				}
				
				node_list_string.trim();
				Debug.println( "read_file parse: " + node_list_string, 35 );
				lines.add( node_list_string ); 
			}
		}catch( IOException ioe ){
			Out.println( "Error! File_Util.read_file:" + ioe );
		}
		
		Debug.println( "read_file finish: \n" + lines, 35 );

		return lines;
	}
	
	// @return  an arraylist of tokens of the specified string
	static public ArrayList parse_string_line( String a_string_line, String token ){
		ArrayList a_string_list = new ArrayList();
		StringTokenizer st = new StringTokenizer( a_string_line, token );

		while (st.hasMoreTokens()) {
			a_string_list.add(st.nextToken());
		}

		return a_string_list;
	}
}